module controller.dept;

import std.conv : to;
import std.algorithm : filter;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.data.json : Json, deserializeJson;
import models.dept;
import repository.dept;
import dtos.dept;
import utils.respdata;
import utils.objmapping;

import std.stdio : writeln;

/** 
 * 获取部门下拉列表
 */
void getDeptOptions(HTTPServerRequest req, HTTPServerResponse resp)
{
    auto allDept = getAllDepts();

    TopDeptOption[] topDepts;
    foreach (dept; allDept)
    {
        if (dept.parentId == 0)
        {
            auto tempSub = filter!(x => x.parentId == dept.id)(allDept);
            SubDeptOption[] temSubs = [];
            foreach (temp; tempSub)
            {
                temSubs ~= new SubDeptOption(temp.id, temp.name);
            }
            topDepts ~= new TopDeptOption(dept.id, dept.name, temSubs);
        }
    }

    resp.writeJsonBody(RespData.newData!(TopDeptOption[])(200, "ok", topDepts));
}

/** 
 * 获取部门列表
 */
void getDeptList(HTTPServerRequest req, HTTPServerResponse resp)
{
    auto allDept = getAllDepts();

    TopDept[] topDepts;
    foreach (dept; allDept)
    {
        if (dept.parentId == 0)
        {
            auto tempSub = filter!(x => x.parentId == dept.id)(allDept);
            SubDept[] temSubs = [];
            foreach (temp; tempSub)
            {
                temSubs ~= new SubDept(temp.id, temp.parentId, temp.name, temp.sort, temp.status);
            }
            topDepts ~= new TopDept(dept.id, dept.parentId, dept.name, dept.sort, dept.status, temSubs);
        }
    }

    resp.writeJsonBody(RespData.newData!(TopDept[])(200, "ok", topDepts));
}

/** 
 * 添加部门
 */
void addDept(HTTPServerRequest req, HTTPServerResponse resp)
{
    // 接收前端数据
    Json json = req.json;
    if (json.length == 0)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    DeptDtoForCreate newDept = deserializeJson!(DeptDtoForCreate)(json);
    if (newDept.name == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    bool res = doAddDept(mappingToObject!(Dept, DeptDtoForCreate)(newDept));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"), 200);
}

/**
* 删除部门
*/
void deleteDept(HTTPServerRequest req, HTTPServerResponse resp)
{
    import std.array : array, split;
    import std.algorithm : map, canFind;

    if ("deptid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    string reqDeptid = req.params["deptid"];
    bool res = false;
    // 删除单个
    if (!canFind(reqDeptid, ","))
    {
        long deptid = to!long(reqDeptid);
        res = deleteDeptById(deptid);
    }
    else
    {
        // 批量删除
        string[] tempids = reqDeptid.split(",");
        long[] deptids = map!(x => to!long(x))(tempids).array();
        res = deleteDeptByIds(deptids);
    }
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"));
}

/**
* 获取部门详情信息
*/
void getDeptInfo(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("deptid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    auto dept = getDeptById(to!long(req.params["deptid"]));
    DeptDto deptDto = mappingToObject!(DeptDto, Dept)(dept);

    resp.writeJsonBody(RespData.newData!(DeptDto)(200, "ok", deptDto));
}

/**
* 编辑部门
*/
void editDept(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("deptid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    long reqDeptid = to!long(req.params["deptid"]);

    // 接收前端json数据
    Json json = req.json;
    if (json.length == 0)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    auto upDept = deserializeJson!(DeptDtoForUpdate)(json);
    if (upDept.name == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    // 提交修改数据
    auto res = doUpdateDept(reqDeptid, mappingToObject!(Dept, DeptDtoForUpdate)(upDept));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"), 200);
}
